package com.example.demo.services.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.jwt.JWTUtil;
import com.example.demo.controller.UserController;
import com.example.demo.domain.po.User;
import com.example.demo.exception.MyException;
import com.example.demo.mapper.UserMapper;
import com.example.demo.services.UserServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class UserServicesImpl implements UserServices {

    @Autowired
    UserMapper userMapper;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    public static String nickname;
    public static Enum role;
    public static Enum userStatus;
    public static int borrow_limit;
    public static int borrow_duration;
    public static int extend_limit;
    public static int extend_duration;

    @Override
    public String login(@RequestParam("username") String username,@RequestParam("password") String password) {
        //检测账号密码是否成立
        User userByInfo = userMapper.getUserByInfo(username, password);

        if (BeanUtil.isEmpty(userByInfo)) {
            throw new MyException("账号或者密码错误，请重试！");
        }
        //获取基础信息（是否管理员，是否封禁）
        nickname = userByInfo.getNickname();
        role = userByInfo.getRole();
        userStatus = userByInfo.getStatus();
        borrow_limit = userByInfo.getBorrow_limit();
        borrow_duration = userByInfo.getBorrow_duration();
        extend_limit = userByInfo.getExtend_limit();
        extend_duration = userByInfo.getExtend_duration();

        Map<String, Object> payload = new HashMap<>();
        payload.put("username", userByInfo.getUsername());

        //添加时间戳,每次登录token将会改变
        payload.put("time",System.currentTimeMillis());
        //生成token，默认是30分钟
        String token = JWTUtil.createToken(payload, "123456".getBytes());

        //将token写入Redis
        ValueOperations<String, String> StringValue = stringRedisTemplate.opsForValue();
        //存入token时需保持相同有效事件
        StringValue.set(username, token,30, TimeUnit.MINUTES);
        System.out.println(username+" 的token已生成");
        return token;
    }


    @Override
    public void logout() {
        stringRedisTemplate.delete(UserController.user);

    }
    //登录方法
    @Override
    public void register(@RequestParam("nickname") String nickname,@RequestParam("username") String username,@RequestParam("password") String password) {
        int i = userMapper.registerUser(nickname,username, password);
        if(i<=0){
            throw new MyException("账号已存在，无法重复注册！");
        }

    }
    //修改密码
    @Override
    public void changePassword(@RequestParam("username") String username,@RequestParam("password") String newPassword) {
        int i = userMapper.updateUser(username, newPassword);
        if(i<=0){
            throw new MyException("修改失败，请检查账号是否存在");
        }

    }
}
